var express = require('express');
var superagent = require('superagent');
var router = express.Router();

// 评论*******************************************************
router.post('/ajax/sendcomment', function(req, res, next) {
  console.log('发送评论');
  console.log(JSON.stringify(req.body));
  superagent
    .post(global.baseURL + '/api/comments')
    .send({
      targetType: req.body.targetType,
      targetId: req.body.targetId,
      content: req.body.content
    })
    .set('DEVICE', 'weixin')
    .set('USER_ID', req.body.userId)
    .set('TOKEN', req.body.token)
    .end(function(err1, res1) {
      if (res1 != null && res1.ok) {
        res.send(res1.body);
      }
      else {
        console.error('请求数据错误! ');
      }
    });
});

// 回复*******************************************************
router.post('/ajax/sendreply', function(req, res, next) {
  console.log('回复');
  console.log(JSON.stringify(req.body));
  superagent
    .post(global.baseURL + '/api/comments')
    .send({
      parentId: req.body.parentId,
      content: req.body.content
    })
    .set('DEVICE', 'weixin')
    .set('USER_ID', req.body.userId)
    .set('TOKEN', req.body.token)
    .end(function(err1, res1) {
      if (res1 != null && res1.ok) {
        res.send(res1.body);
      }
      else {
        console.error('请求数据错误! ');
      }
    });
});

// 刷新*******************************************************
router.get('/ajax/refresh', function(req, res, next) {
  console.log('刷新');
  superagent
    .get(global.baseURL + '/api/comments')
    .query({
      targetType: req.query.targetType,
      targetId: req.query.targetId,
      pageNumber:req.query.pageNumber
    })
    .set('DEVICE', 'weixin')
    .end(function(err1, res1) {
      if (res1 != null && res1.ok) {
        res.send(res1.body);
      }
      else {
        console.error('请求数据错误! ');
      }
    });
});

module.exports = router;
